﻿//  -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Tests
//  File name:	TestConfiguration.cs
//  Developer:	Solomatov Igor
//  Created:	03.04.2012
// 
//  -------------------- iSharePoint -------------------- //

using System.Diagnostics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Smart.Common;

namespace Smart.Documents.Tests
{
	[TestClass]
	public class TestConfiguration
	{
		[TestMethod]
		public void ConfigurationSetGetValue()
		{
			string cv1 = null;
			string cv2 = null;
			string cv3 = null;
			string cv11 = null;
			string cv12 = null;
			string cv13 = null;
			string cv14 = null;
			string cv21 = null;
			string cv22 = null;
			string cv23 = null;
			string cv24 = null;
			string cv25 = null;
			string cv26 = null;
			string cv27 = null;

			string acv1 = null;
			string acv2 = null;
			string acv3 = null;
			string acv4 = null;
			string acv5 = null;
			string acv6 = null;
			string acv7 = null;
			string acv8 = null;
			string acv9 = null;
			string acv10 = null;
			string acv11 = null;
			string acv12 = null;
			string acv13 = null;
			string acv14 = null;
			string acv15 = null;
			string acv16 = null;
			string acv17 = null;
			string acv18 = null;
			string acv19 = null;
			string acv20 = null;

			var sw1_nc = new Stopwatch();
			var sw2_c = new Stopwatch();

			var asw1_nc = new Stopwatch();
			var asw2_c = new Stopwatch();
			var asw3_nc = new Stopwatch();
			var asw4_c = new Stopwatch();

			var res = SPH.RunUnder("http://localhost:777/",
								   delegate
								   {
									   var site = SPH.GetSite();
									   var web = SPH.GetWeb();

									   SPServices.Configuration.SetValue(web, "title", "value");

									   cv1 = SPServices.Configuration.GetValue("title", "defaultValue");
									   cv11 = SPServices.Configuration.GetValue("title", "defaultValue");
									   cv12 = SPServices.Configuration.GetValue("title", "defaultValue");

									   SPServices.Configuration.SetUserValue("userKey", "userValue");

									   cv2 = SPServices.Configuration.GetValue("userKey", "error");

									   SPServices.Configuration.SetUserValue("userKey1", "userValue1");

									   cv21 = SPServices.Configuration.GetValue("userKey1", "error");
									   cv22 = SPServices.Configuration.GetValue("userKey1", "error");

									   SPServices.Configuration.SetUserValue("userKey2", "userValue2");

									   cv23 = SPServices.Configuration.GetValue("userKey2", "error");
									   cv24 = SPServices.Configuration.GetValue("userKey2", "error");

									   SPServices.Configuration.SetValue(web, "webKey", "webValue");

									   cv3 = SPServices.Configuration.GetValue(site, web, null, null, "webKey", "webDefaultValue");

									   H.LT("************ Caching test: loading non cached data ************");

									   sw1_nc.Start();
									   cv13 = SPServices.Configuration.GetValue("title", "defaultValue");
									   sw1_nc.Stop();

									   H.LT("************ Caching test: loading cached data ************");

									   sw2_c.Start();
									   cv14 = SPServices.Configuration.GetValue("title", "defaultValue");
									   cv25 = SPServices.Configuration.GetValue("userKey", "error");
									   cv26 = SPServices.Configuration.GetValue("userKey1", "error");
									   cv27 = SPServices.Configuration.GetValue("userKey2", "error");
									   sw2_c.Stop();

									   H.LT("************ Cache timings, non-cached: {0}, cached: {1} ************", sw1_nc.Elapsed, sw2_c.Elapsed);

									   asw1_nc.Start();
									   acv1 = SPServices.Configuration.GetValue(site, web, null, null, "nullKey", "webDefaultValue");
									   asw1_nc.Stop();

									   asw2_c.Start();
									   acv2 = SPServices.Configuration.GetValue(site, web, null, null, "nullKey", "webDefaultValue");
									   asw2_c.Stop();

									   H.LT("************ Cache timings, non-cached: {0}, cached: {1} ************", asw1_nc.Elapsed, asw2_c.Elapsed);

									   SPServices.Cache.Clear();

									   asw3_nc.Start();
									   acv3 = SPServices.Configuration.GetValue(site, web, null, null, "nullKey", "webDefaultValue");
									   asw3_nc.Stop();

									   asw4_c.Start();
									   acv4 = SPServices.Configuration.GetValue(site, web, null, null, "nullKey", "webDefaultValue");
									   asw4_c.Stop();

									   H.LT("************ Cache timings, non-cached: {0}, cached: {1} ************", asw3_nc.Elapsed, asw4_c.Elapsed);

									   SPServices.Configuration.SetValue(web.CurrentUser, "WebWithCurrentUserKey", "webWithCurrentUserValue");

									   acv5 = SPServices.Configuration.GetValue(site, web, null, web.CurrentUser, "WebWithCurrentUserKey", "webDefaultValue");

									   SPServices.Configuration.SetValue(web.CurrentUser, "WebWithCurrentUserKey", "webWithCurrentUserValueChanged");

									   acv6 = SPServices.Configuration.GetValue(site, web, null, web.CurrentUser, "WebWithCurrentUserKey", "webDefaultValue");

									   SPServices.Configuration.SetValue(web, "WebWithWebKey", "webWithCurrentUserValueChanged");

									   acv7 = SPServices.Configuration.GetValue(site, web, null, null, "WebWithCurrentUserKey", "webDefaultValue");

									   SPServices.Configuration.SetWebValue(web.ID, "WebWithGuid", "webWithGuidValue");

									   acv8 = SPServices.Configuration.GetValue(site, web, null, null, "WebWithGuid", "webDefaultValue");

									   var list = web.GetListByUrl("/Lists/msgqueue");

									   SPServices.Configuration.SetListValue(list.ID, "ListWithGuid", "listWithGuidValue");

									   acv9 = SPServices.Configuration.GetValue(site, web, list, null, "ListWithGuid", "listDefaultValue");

									   acv10 = SPServices.Configuration.GetValue(site, web, null, null, "ListWithGuid", "listDefaultValue");

									   SPServices.Configuration.RemoveListValue(list.ID, "ListWithGuid");

									   acv11 = SPServices.Configuration.GetValue(site, web, list, null, "ListWithGuid", "listDefaultValue");

									   SPServices.Configuration.RemoveWebValue(web.ID, "webKey");

									   acv12 = SPServices.Configuration.GetValue(site, web, null, null, "webKey", "webDefaultValue");

									   SPServices.Configuration.RemoveValue(web, "title");

									   acv13 = SPServices.Configuration.GetValue("webKey", "webDefaultValue");

									   SPServices.Configuration.RemoveValue(web.CurrentUser, "userKey");

									   acv14 = SPServices.Configuration.GetValue("userKey", "userDefaultValue");

									   SPServices.Configuration.RemoveValue(web.CurrentUser, "userKey1");

									   acv15 = SPServices.Configuration.GetValue("userKey1", "userDefaultValue");

									   SPServices.Configuration.RemoveValue(web.CurrentUser, "userKey2");

									   acv16 = SPServices.Configuration.GetValue("userKey2", "userDefaultValue");

									   SPServices.Configuration.RemoveValue(web.CurrentUser, "WebWithCurrentUserKey");

									   acv17 = SPServices.Configuration.GetValue("WebWithCurrentUserKey", "userDefaultValue");

									   SPServices.Configuration.RemoveValue(web, "WebWithWebKey");

									   acv18 = SPServices.Configuration.GetValue("WebWithWebKey", "webDefaultValue");

									   SPServices.Configuration.RemoveValue(web, "WebWithGuid");

									   acv19 = SPServices.Configuration.GetValue("WebWithGuid", "webDefaultValue");

									   SPServices.Configuration.RemoveValue(web.CurrentUser, "WebWithGuid");

									   acv20 = SPServices.Configuration.GetValue("WebWithGuid", "webDefaultValue");
								   });

			Assert.AreEqual(true, res, "SPH.RunUnder completed with errors!");

			Assert.AreEqual("value", cv1, "Method SetValue or GetValue is not working!");
			Assert.AreEqual("value", cv11, "Method SetValue or GetValue is not working! (cached call)");
			Assert.AreEqual("value", cv12, "Method SetValue or GetValue is not working! (cached call)");
			Assert.AreEqual("value", cv13, "Method SetValue or GetValue is not working!");
			Assert.AreEqual("value", cv14, "Method SetValue or GetValue is not working! (cached call)");
			Assert.AreEqual("userValue", cv2, "Method SetValue or GetValue is not working!");
			Assert.AreEqual("userValue1", cv21, "Method SetValue or GetValue is not working!");
			Assert.AreEqual("userValue1", cv22, "Method SetValue or GetValue is not working! (cached call)");
			Assert.AreEqual("userValue2", cv23, "Method SetValue or GetValue is not working!");
			Assert.AreEqual("userValue2", cv24, "Method SetValue or GetValue is not working! (cached call)");
			Assert.AreEqual("userValue", cv25, "Method SetValue or GetValue is not working! (cached call)");
			Assert.AreEqual("userValue1", cv26, "Method SetValue or GetValue is not working! (cached call)");
			Assert.AreEqual("userValue2", cv27, "Method SetValue or GetValue is not working! (cached call)");
			Assert.AreEqual("webValue", cv3, "Method SetValue or GetValue is not working!");

			Assert.AreEqual("webDefaultValue", acv1, "Method SetValue or GetValue is not working!");
			Assert.AreEqual("webWithCurrentUserValue", acv5, "Method SetValue or GetValue is not working!");
			Assert.AreEqual("webWithCurrentUserValueChanged", acv6, "Method Set value is not working!");
			Assert.AreEqual("webDefaultValue", acv7, "Method Set value is not working!");
			Assert.AreEqual("webWithGuidValue", acv8, "Method Set value is not working!");
			Assert.AreEqual("listWithGuidValue", acv9, "Method Set value is not working!");
			Assert.AreEqual("listDefaultValue", acv10, "Method Set value is not working!");
			Assert.AreEqual("listDefaultValue", acv11, "Method RemoveValue is not working!");
			Assert.AreEqual("webDefaultValue", acv12, "Method RemoveValue is not working!");
			Assert.AreEqual("webDefaultValue", acv13, "Method RemoveValue is not working!");
			Assert.AreEqual("userDefaultValue", acv14, "Method RemoveValue is not working!");
			Assert.AreEqual("userDefaultValue", acv15, "Method RemoveValue is not working!");
			Assert.AreEqual("userDefaultValue", acv16, "Method RemoveValue is not working!");
			Assert.AreEqual("userDefaultValue", acv17, "Method RemoveValue is not working!");
			Assert.AreEqual("webDefaultValue", acv18, "Method RemoveValue is not working!");
			Assert.AreEqual("webDefaultValue", acv19, "Method RemoveValue is not working!");
			Assert.AreEqual("webDefaultValue", acv20, "Method RemoveValue is not working!");

			Assert.AreEqual(true, sw1_nc.Elapsed.TotalMilliseconds > sw2_c.Elapsed.TotalMilliseconds * 10,
							string.Format("Cache is not working, non-cached: {0}, cached: {1}", sw1_nc.Elapsed, sw2_c.Elapsed));
			Assert.AreEqual(true, asw1_nc.Elapsed.TotalMilliseconds > asw2_c.Elapsed.TotalMilliseconds * 10,
							string.Format("Cache is not working, non-cached: {0}, cached: {1}", asw1_nc.Elapsed, asw2_c.Elapsed));
			Assert.AreEqual(true, asw3_nc.Elapsed.TotalMilliseconds > asw4_c.Elapsed.TotalMilliseconds * 10,
							string.Format("Cache is not working, non-cached: {0}, cached: {1}", asw3_nc.Elapsed, asw4_c.Elapsed));
		}

		[TestMethod]
		public void ConfigurationSetGetValueWithoutCache()
		{
			string acv1 = null;
			string acv2 = null;
			string acv3 = null;
			string acv4 = null;
			string acv5 = null;
			string acv6 = null;
			string acv7 = null;
			string acv8 = null;
			string acv9 = null;
			string acv10 = null;
			string acv11 = null;
			string acv12 = null;
			string acv13 = null;
			string acv14 = null;
			string acv15 = null;
			string acv16 = null;
			string acv17 = null;
			string acv18 = null;
			string acv19 = null;

			var res = SPH.RunUnder("http://localhost:777/",
								   delegate
								   {
									   var site = SPH.GetSite();
									   var web = SPH.GetWeb();

									   SPServices.Cache.Clear();

									   acv1 = SPServices.Configuration.GetValue(site, web, null, null, "nullKey", "webDefaultValue");

									   SPServices.Cache.Clear();

									   acv2 = SPServices.Configuration.GetValue(site, web, null, null, "nullKey", "webDefaultValue");

									   SPServices.Cache.Clear();

									   acv3 = SPServices.Configuration.GetValue(site, web, null, null, "nullKey", "webDefaultValue");

									   SPServices.Cache.Clear();

									   acv4 = SPServices.Configuration.GetValue(site, web, null, null, "nullKey", "webDefaultValue");

									   SPServices.Cache.Clear();

									   SPServices.Configuration.SetValue(web.CurrentUser, "WebWithCurrentUserKey", "webWithCurrentUserValue");

									   SPServices.Cache.Clear();

									   acv5 = SPServices.Configuration.GetValue(site, web, null, web.CurrentUser, "WebWithCurrentUserKey", "webDefaultValue");

									   SPServices.Cache.Clear();

									   SPServices.Configuration.SetValue(web.CurrentUser, "WebWithCurrentUserKey", "webWithCurrentUserValueChanged");

									   SPServices.Cache.Clear();

									   acv6 = SPServices.Configuration.GetValue(site, web, null, web.CurrentUser, "WebWithCurrentUserKey", "webDefaultValue");

									   SPServices.Cache.Clear();

									   SPServices.Configuration.SetValue(web, "WebWithCurrentUserKey", "webWithCurrentUserValueChanged");

									   SPServices.Cache.Clear();

									   acv7 = SPServices.Configuration.GetValue(site, web, null, null, "WebWithCurrentUserKey", "webDefaultValue");

									   SPServices.Cache.Clear();

									   SPServices.Configuration.SetWebValue(web.ID, "WebWithGuid", "webWithGuidValue");

									   SPServices.Cache.Clear();

									   acv8 = SPServices.Configuration.GetValue(site, web, null, null, "WebWithGuid", "webDefaultValue");

									   SPServices.Cache.Clear();

									   var list = web.GetListByUrl("/Lists/msgqueue");

									   SPServices.Cache.Clear();

									   SPServices.Configuration.SetListValue(list.ID, "ListWithGuid", "listWithGuidValue");

									   SPServices.Cache.Clear();

									   acv9 = SPServices.Configuration.GetValue(site, web, list, null, "ListWithGuid", "listDefaultValue");

									   SPServices.Cache.Clear();

									   acv10 = SPServices.Configuration.GetValue(site, web, null, null, "ListWithGuid", "listDefaultValue");

									   SPServices.Configuration.RemoveListValue(list.ID, "ListWithGuid");

									   SPServices.Cache.Clear();

									   acv11 = SPServices.Configuration.GetValue(site, web, list, null, "ListWithGuid", "listDefaultValue");

									   SPServices.Configuration.RemoveWebValue(web.ID, "webKey");

									   SPServices.Cache.Clear();

									   acv12 = SPServices.Configuration.GetValue(site, web, null, null, "webKey", "webDefaultValue");

									   SPServices.Cache.Clear();

									   SPServices.Configuration.RemoveValue(web, "title");

									   SPServices.Cache.Clear();

									   acv13 = SPServices.Configuration.GetValue("webKey", "webDefaultValue");

									   SPServices.Cache.Clear();

									   SPServices.Configuration.RemoveValue(web.CurrentUser, "userKey");

									   SPServices.Cache.Clear();

									   acv14 = SPServices.Configuration.GetValue("userKey", "userDefaultValue");

									   SPServices.Cache.Clear();

									   SPServices.Configuration.RemoveValue(web.CurrentUser, "userKey1");

									   SPServices.Cache.Clear();

									   acv15 = SPServices.Configuration.GetValue("userKey1", "userDefaultValue");

									   SPServices.Cache.Clear();

									   SPServices.Configuration.RemoveValue(web.CurrentUser, "userKey2");

									   SPServices.Cache.Clear();

									   acv16 = SPServices.Configuration.GetValue("userKey2", "userDefaultValue");

									   SPServices.Cache.Clear();

									   SPServices.Configuration.RemoveValue(web, "WebWithCurrentUserKey");

									   SPServices.Cache.Clear();

									   SPServices.Configuration.RemoveValue(web.CurrentUser, "WebWithCurrentUserKey");

									   SPServices.Cache.Clear();

									   acv17 = SPServices.Configuration.GetValue("WebWithCurrentUserKey", "userDefaultValue");

									   SPServices.Cache.Clear();

									   SPServices.Configuration.RemoveValue(web.CurrentUser, "WebWithWebKey");

									   SPServices.Cache.Clear();

									   acv18 = SPServices.Configuration.GetValue("WebWithWebKey", "webDefaultValue");

									   SPServices.Cache.Clear();

									   SPServices.Configuration.RemoveValue(web.CurrentUser, "WebWithGuid");

									   SPServices.Cache.Clear();

									   acv19 = SPServices.Configuration.GetValue("WebWithGuid", "webDefaultValue");

									   SPServices.Configuration.RemoveValue(web, "WebWithGuid");
								   });

			Assert.AreEqual("webDefaultValue", acv1, "Method SetValue or GetValue is not working!");
			Assert.AreEqual("webWithCurrentUserValue", acv5, "Method SetValue or GetValue is not working!");
			Assert.AreEqual("webWithCurrentUserValueChanged", acv6, "Method Set value is not working!");
			Assert.AreEqual("webWithCurrentUserValueChanged", acv7, "Method Set value is not working!");
			Assert.AreEqual("webWithGuidValue", acv8, "Method Set value is not working!");
			Assert.AreEqual("listWithGuidValue", acv9, "Method Set value is not working!");
			Assert.AreEqual("listDefaultValue", acv10, "Method Set value is not working!");
			Assert.AreEqual("listDefaultValue", acv11, "Method RemoveValue is not working!");
			Assert.AreEqual("webDefaultValue", acv12, "Method RemoveValue is not working!");
			Assert.AreEqual("webDefaultValue", acv13, "Method RemoveValue is not working!");
			Assert.AreEqual("userDefaultValue", acv14, "Method RemoveValue is not working!");
			Assert.AreEqual("userDefaultValue", acv15, "Method RemoveValue is not working!");
			Assert.AreEqual("userDefaultValue", acv16, "Method RemoveValue is not working!");
			Assert.AreEqual("userDefaultValue", acv17, "Method RemoveValue is not working!");
			Assert.AreEqual("webDefaultValue", acv18, "Method RemoveValue is not working!");
			Assert.AreEqual("webWithGuidValue", acv19, "Method RemoveValue is not working!");
		}
	}
}